<?php
namespace App\Model;
use App\Repository\Common\ConstTable;
use App\Repository\Sys\Db;
use App\Repository\Sys\JsonRespose;
use App\Repository\Sys\Str;
//行业表
class IndustryTable{
    use Db;
    static private $table = 'industry';
    //行业-列表
    static public function getIndustryList($page,$limit,$title,$status){
    	$offset = ($page - 1) * $limit;
        //拼接sql
        $where = "1=1";
        $parameters = array(
            'limit' => $limit,
            'offset' => $offset
        );
        if ($title) {
            $where .= " AND  d.title LIKE :title ";
            $parameters['title'] = $title;
        }
        if ($status) {
            $where .= " AND d.status = :status ";
            $parameters['status'] = $status;
            $sql = "select * from industry as d  WHERE {$where} ORDER BY d.sort_top DESC,d.sort ASC,d.id DESC limit  :offset,:limit;";
        } else {
            $where .= " AND d.status>0";
            $sql = "select * from industry as d  WHERE {$where} ORDER BY d.sort_top DESC,d.sort DESC,d.id DESC limit  :offset,:limit;";
        }
        //执行sql
        $res = \DB::select($sql, $parameters);
        unset($parameters['limit'], $parameters['offset']);
        $count = \DB::select("select count(*) as c from industry as d WHERE {$where}", $parameters)[0]->c;
        $data=[];
        foreach ($res as $v) {
        	$v->updated_at = Str::dateFormat('Y-m-d', $v->updated_at);
        	$v->type_text=ConstTable::JOIN_TYPE[$v->type];
        	$v->status_text=ConstTable::ADMIN_STATUS_NAME[$v->status];
        	$data[] = $v;
        }
        return JsonRespose::jsonArray(1,[
            'count' => $count,
            'limit' => $limit,
            'page' => $page,
            'res' => $data
        ]);
    }
    //行业-详情
    static public function getIndustryInfo($id){
        $res = IndustryTable::ORM()->where('id', $id)->first();
        if (!$res) {
            return JsonRespose::fail('数据不存在');
        }
        $data = [
            'id' => $res->id,
            'title' => $res->title,
            'img_url' => $res->img_url,
            'img_sm_url' => $res->img_sm_url,
            'content' => $res->content,
            'content_array' => Str::explodeToArray($res->content)
        ];
        return JsonRespose::jsonArray(1,$data);
    }
}